package fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import fastjson.po.MyData;

public class JsonStringToJavaBean {
    public static void main(String[] args) {
        String s = "{\"action\":\"add\",\"id\":\"1\",\"ordinal\":8,\"organUnitFullName\":\"testJSON\",\"parent\":\"0\",\"suborderNo\":\"58961\"}";
        MyData data = JSON.parseObject(s, MyData.class);
        System.out.println("data对象" + data.toString());
        System.out.println("action=" + data.getAction() + "---id=" + data.getId());
        //data对象Data{id='1', suborderNo='58961', organUnitType='null', action='add', parent='0', organUnitFullName='testJSON', ordinal=8}
        //action=add---id=1

        /**
         * 另一种方式转对象
         */
        MyData dd = JSON.parseObject(s, new TypeReference<MyData>() {
        });
        System.out.println("另一种方式获取data对象" + dd.toString());
        System.out.println("另一种方式获取=" + dd.getAction() + "---id=" + dd.getId());
        //另一种方式获取data对象Data{id='1', suborderNo='58961', organUnitType='null', action='add', parent='0', organUnitFullName='testJSON', ordinal=8}
        //另一种方式获取=add---id=1
    }
}
